version 13
#delimit;
clear all;
set more off;
capture log close;


global strformat "%8.3f";

capture program drop sig_p;
program sig_p;
   args obj_var point_est_var point_sd_var p_val row_id_var row_num;
   local point_est `point_est_var';
   local point_sd `point_sd_var';
   if `p_val' > 0.1 {;
      replace `obj_var' = string(`point_est', "$strformat") if `row_id_var' == `row_num';
      };
   if `p_val' > 0.05 & `p_val' <= 0.1  {;
      replace `obj_var' = string(`point_est', "$strformat")+"*" if `row_id_var' == `row_num';
      };
   if `p_val' > 0.01 & `p_val' <= 0.05  {;
      replace `obj_var' = string(`point_est', "$strformat")+"**" if `row_id_var' == `row_num';
      };
   if `p_val' <= 0.01 {;
      replace `obj_var' = string(`point_est', "$strformat")+"***" if `row_id_var' == `row_num';
      };
   replace `obj_var' = "<" + string(`point_sd', "$strformat") + ">" + " " if `row_id_var' == `row_num' + 1;
   end;


*enter directory name here;

use "$dir/ReStat_children.dta", clear;


	

drop if vill_school_level ~= 1;
bysort villcode: gen vill_level = _n;


foreach var of varlist 
	school_type_govt school_type_pprs school_type_priv school_type_ngo {;

	replace `var' = 0 if pooled_treatment == 0 & total_enrolled == 0;

	bysort villcode: egen t_`var' = total(`var');
	gen dt_`var' = (t_`var' > 0) if t_`var' ~= .;
	
	gen p_`var' = `var' if m_distance_hh_to_school < 1.5;
	replace p_`var' = 0 if  m_distance_hh_to_school >= 1.5;
	bysort villcode: egen tp_`var' = total(p_`var');
	gen dtp_`var' = (tp_`var' > 0) if tp_`var' ~= .;
		
	gen enrol_`var' = total_enrolled if `var' == 1;
	bysort villcode: egen t_enrol_`var' = total(enrol_`var');
	gen dt_enrol_`var' = (t_enrol_`var' > 0) if t_enrol_`var' ~= .;
	
};



forvalues i=0/35 {;
	gen col`i' = "";
};

gen row_num = _n;
local row = 3;

order col*;
sort row_num;


foreach type in pprs govt priv ngo {;

	replace col0 = "`type'" if row_num == `row';

	su dtp_school_type_`type' if vill_level == 1 & pooled_treatment == 0;
	replace col2 = string(r(mean), "$strformat") if row_num == `row';
	
	su dtp_school_type_`type' if vill_level == 1 & pooled_treatment == 1;
	replace col3 = string(r(mean), "$strformat") if row_num == `row';

	reg dtp_school_type_`type' pooled_treatment if vill_level == 1;
	
	

		
	su dt_enrol_school_type_`type' if vill_level == 1 & pooled_treatment == 0;
	replace col5 = string(r(mean), "$strformat") if row_num == `row';
	
	su dt_enrol_school_type_`type' if vill_level == 1 & pooled_treatment == 1;
	replace col6 = string(r(mean), "$strformat") if row_num == `row';
	
	reg dt_enrol_school_type_`type' pooled_treatment if vill_level == 1;

	
	
	su t_enrol_school_type_`type' if vill_level == 1 & pooled_treatment == 0;
	replace col8 = string(r(mean), "$strformat") if row_num == `row';
	
	su t_enrol_school_type_`type' if vill_level == 1 & pooled_treatment == 1;
	replace col9 = string(r(mean), "$strformat") if row_num == `row';
	
	reg t_enrol_school_type_`type' pooled_treatment if vill_level == 1;

	
	local row = `row' + 3;
	
};

local row = `row' + 1;


	
outsheet col0-col9 using "$dir/Table A3 school statistics.csv" if row_num < `row', 
	comma replace;

